package com.jms.fu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jms.fu.exception.DAOException;
import com.jms.fu.model.bean.Comment;
import com.jms.fu.model.bo.AppBO;

public class GetCommentAction extends Action {
	private static Logger logger = Logger.getLogger(LoginAction.class);

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AppBO appbo = new AppBO();
		int appid = 0;

		try {
			appid = Integer.parseInt(request.getParameter("appid"));
		} catch (NumberFormatException e) {
			logger.error(e.getMessage());
		}

		try {
			ArrayList<Comment> listComment = appbo.getCommentByAppId(appid);
			String result = "";
			int num = listComment.size();
			if (!(num == 0)) {
				for (int i = 0; i < num; i++) {
					result += "<li class='list-group-item'>"
							+ "<div class='row'>"
									+ "<div class='col-xs-2 col-md-2'>"
											+ "<img src='http://placehold.it/80' class='img-circle img-responsive' alt=''></div>"
											+ "<div class='col-xs-10 col-md-10' style='text-align:left'>"
											+ "<div><h3>"
											+ listComment.get(i).getCon_tent() +"</h3>"
											+ "<div class='mic-info'>"
											+ "By: <a href='viewUser.jsm?usernameCheck="+listComment.get(i).getUserId()+"&roleCheck=2'>"+listComment.get(i).getUserId()+"</a> on "+listComment.get(i).getDateComment()
											+ "</div>"
											+ "</div>"
											+ "</div></div></li>";
				}
			}

			PrintWriter out = response.getWriter();
			out.print(result);
			out.close();
		} catch (DAOException | IOException e) {
			logger.error(e.getMessage());
		}
		return null;
	}
}
